<template>
  <div>
    <el-row class="warp">
      <el-col :span="24" class="warp-breadcrum">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/' }"><b>系统管理</b></el-breadcrumb-item>
          <el-breadcrumb-item>资源管理</el-breadcrumb-item>
        </el-breadcrumb>
      </el-col>

      <el-col :span="24" class="warp-main" v-loading="loading" element-loading-text="拼命加载中">
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
          <el-form :inline="true" :model="filters">
            <el-form-item>
              <el-input v-model="filters.name" placeholder="资源名" style="min-width: 240px;"
                        @keyup.enter.native="handleSearch"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleSearch">查询</el-button>
            </el-form-item>
            <el-form-item style="float: right">
              <el-button type="primary" @click="openDomainDialog()">新增</el-button>
            </el-form-item>
          </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="domainList" highlight-current-row v-loading="loading" style="width: 100%;">
          <el-table-column type="index" width="60">
          </el-table-column>
          <el-table-column prop="name" label="资源名称" sortable>
          </el-table-column>
          <el-table-column prop="url" label="资源路径" sortable>
          </el-table-column>
          <el-table-column prop="code" label="编码" sortable>
          </el-table-column>
          <el-table-column   label="是否子节点" sortable>
            <template scope="scope">
              {{ scope.row.isLeaf? '是':'否'}}
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="描述" width="100">
          </el-table-column>
          <el-table-column property="operate" label="操作" width="150">
            <template scope="scope">
              <el-button
                size="mini" type="primary" @click="openEditDomainDialog(scope.row)">编辑
              </el-button>
              <el-button
                size="mini"
                type="danger" @click="delDomain(scope.row.id)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-row>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNumber"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="1"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.totalCount">
      </el-pagination>
    </el-row>
    <el-dialog :title="domainDialog.title" :visible.sync="domainDialog.isShow" width="450px" :close-on-click-modal="false" @close="closeDomainDialogFn"
               :modal="true">
      <el-form :model="domainForm" :rules="rules" ref="domainForm">
        <el-form-item prop="roleName" label="资源名称" label-width="100px">
          <el-input size="medium" placeholder="请输入资源名称" v-model="domainForm.name" style="width: 217px"></el-input>
        </el-form-item>
        <el-form-item prop="code" label="资源编码" label-width="100px" style="margin-top: -10px">
          <el-input size="medium" placeholder="请输入资源路径" v-model="domainForm.code" style="width: 217px"></el-input>
        </el-form-item>
        <el-form-item prop="url" label="资源URL" label-width="100px" style="margin-top: -10px">
          <el-input size="medium" placeholder="请输入URL" v-model="domainForm.url" style="width: 217px"></el-input>
        </el-form-item>
        <el-form-item prop="isLeaf" label="是否子节点" label-width="100px" style="margin-top: -10px">
          <el-select v-model="domainForm.isLeaf" placeholder="请选择">
            <el-option :value="true" label="是"></el-option>
            <el-option :value="false" label="否"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="remark" label="备注" label-width="100px" style="margin-top: -10px">
          <el-input size="medium" placeholder="请输入备注" v-model="domainForm.remark" style="width: 217px"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" type="default" @click="closeDialogFn(domainDialog.isShow = false)">取消</el-button>
        <el-button size="medium" type="primary" @click="submitDomainForm('domainForm')">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import domainAPI from '../../api/api_resource';

  export default {
    data() {
      return {
        page: {
          pageSize: 10,
          pageNumber: 1,
          totalCount: 0
        },
        filters: {
          name: ''
        },
        domainDialog: {
          isShow: false,
          title: '资源新增'
        },
        domainForm: {
          name: '',
          remark: '',
          isLeaf: true,
          code: '',
          url: ''
        },
        resetDomainForm: {
          name: '',
          remark: '',
          isLeaf: true,
          code: '',
          url: ''
        },
        rules: {
          email: [{required: true, type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change']}]
        },
        loading: false,
        domainList: [],
        loading: false
      }
    },
    methods: {
      closeDialogFn() {
        this.domainDialog.isShow = false
      },
      submitDomainForm(formName) {
        let _this = this
        this.$refs[formName].validate((valid) => {
          if (valid) {
            domainAPI.doAddOrUpdate(this.domainForm).then(result => {
              _this.$message({
                message: result.message,
                type: 'success'
              })
              _this.handleSearch()
              _this.domainDialog.isShow = false
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      openEditDomainDialog (row) {
        this.domainDialog.isShow = true
        this.domainDialog.title = '编辑资源'
        Object.assign(this.domainForm, row)
      },
      delDomain (id) {
        let _this = this
        this.$confirm('此操作将删除资源, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          domainAPI.doDel({'id': id}).then(result => {
            _this.$message({
              message: result.message,
              type: 'success'
            })
            _this.handleSearch()
          })
        }).catch(() => {
        })
      },
      openDomainDialog () {
        this.domainDialog.isShow = true
        this.domainDialog.title = '新增资源'
        this.domainForm.id = -1
      },
      closeDomainDialogFn () {
        this.$refs['domainForm'].resetFields()
        Object.assign(this.domainForm, this.resetDomainForm)
      },
      handleSearch() {
        this.page.totalCount = 0;
        this.page.pageNumber = 1;
        this.search();
      },
      //获取资源列表
      search: function () {
        let that = this;
        let params = {
          name: that.filters.name
        };
        Object.assign(params, this.page)
        that.loading = true;
        domainAPI.qryByPage(params).then(function (result) {
          that.loading = false;
          if (result) {
            that.page.totalCount = result.data.totalCount;
            that.domainList = result.data.result;
          }
        }, function (err) {
          that.loading = false;
          that.$message.error({showClose: true, message: err.toString(), duration: 2000});
        }).catch(function (error) {
          that.loading = false;
          that.$message.error({showClose: true, message: '请求出现异常', duration: 2000});
        });
      },
      handleSizeChange (pageSize) {
        this.page.pageSize = pageSize
        this.search()
      },
      handleCurrentChange(pageNumber){
        this.page.pageNumber = pageNumber
        this.search()
      }
    },
    mounted() {
      this.handleSearch()
    }
  }
</script>

<style scoped>

</style>
